package com.atguigu.flink.day04;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/8/13
 * 该案例演示了分流-filter
 * 需求：读取一个整数数字流，将数据流划分为奇数流和偶数流
 */
public class Flink03_Split_Filter {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
        //Configuration conf = new Configuration();
        //conf.set(RestOptions.PORT,8088);
        //StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        //TODO 2.从指定的网络端口读取数据并将其转换为数字流
        SingleOutputStreamOperator<Integer> socketDS = env
                .socketTextStream("hadoop102", 8888)
                .map(Integer::valueOf);
        //TODO 3.过滤出奇数流
        SingleOutputStreamOperator<Integer> ds1 = socketDS.filter(num -> num % 2 == 1).setParallelism(2);
        //TODO 4.过滤出偶数流
        SingleOutputStreamOperator<Integer> ds2 = socketDS.filter(num -> num % 2 == 0).setParallelism(3);

        //TODO 5.打印
        ds1.print("奇数:");
        ds2.print("偶数:");
        //TODO 6.提交
        env.execute();

    }
}
